package com.bluesmart.mia;

import java.util.Date;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.sys.DemoApp;
import com.util.Constant;
import com.util.ImageLoaderHelper;
import com.util.MgqDataHandler;
import com.util.MgqRestClient;
import com.util.PreferencesUtils;
import com.util.Utils;
import com.widget.CircleImageView;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

public class ChatViewAtivity extends FragmentActivity implements OnClickListener{
	HistoryViewActivity historyViewActivity = null;
	BabyChatActivity babyChatActivity = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_chat_view);
		
		((TextView)findViewById(R.id.center)).setText(getString(R.string.history));
		
		findViewById(R.id.icon).setOnClickListener(this);
		findViewById(R.id.right).setOnClickListener(this);
		findViewById(R.id.btn0).setOnClickListener(this);
		findViewById(R.id.btn1).setOnClickListener(this);
		
		FragmentManager fm = getFragmentManager();  
        FragmentTransaction transaction = fm.beginTransaction();  
        historyViewActivity = new HistoryViewActivity();  
        transaction.replace(R.id.id_fragment_content, historyViewActivity);  
        transaction.commit();  

		((Button)findViewById(R.id.btn0)).setBackgroundColor(Color.rgb(13,128,214));
		((Button)findViewById(R.id.btn0)).setTextColor(Color.WHITE);
        
        initRightMenu();
	}
	
	protected void onResume()
	{
		super.onResume();
		setUserImage();
		if(((DemoApp)getApplicationContext()).isNewMsg)
		{
			((TextView)slidingMenu.findViewById(R.id.btn5)).setCompoundDrawablesWithIntrinsicBounds(
					R.drawable.bg_news, 0, R.drawable.a17, 0);
			((ImageButton)findViewById(R.id.right)).setImageResource(R.drawable.unread);
		}
		else
		{
			((TextView)slidingMenu.findViewById(R.id.btn5)).setCompoundDrawablesWithIntrinsicBounds(
					0, 0, R.drawable.a17, 0);
			((ImageButton)findViewById(R.id.right)).setImageResource(R.drawable.a25);
		}
	}
	
	void setUserImage()
	{
		if(((DemoApp)getApplicationContext()).mBabyList != null)
		{
			JSONObject obj = ((DemoApp)getApplicationContext()).getSeletedBaby();
			if(obj != null)
			{
				TextView tv = (TextView)slidingMenu.findViewById(R.id.btn10);
				tv.setTag(obj.toString());
				
				ImageLoaderHelper.displayImage(R.drawable.a53, (CircleImageView)findViewById(R.id.icon), 
						obj.getString("image"));
			}
		}
	}

	SlidingMenu slidingMenu = null;
	void initRightMenu()
	{
		OnClickListener listener = new OnClickListener(){
			@Override
			public void onClick(View arg0) {
				menuBtn(arg0);
			}
		};
		
		slidingMenu = new SlidingMenu(this);
		slidingMenu.setMode(SlidingMenu.RIGHT);
		slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE);
		slidingMenu.setShadowDrawable(R.drawable.shadow);
		slidingMenu.setShadowWidthRes(R.dimen.shadow_width);
		slidingMenu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
		slidingMenu.setMenu(R.layout.right_menu);
		slidingMenu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
		slidingMenu.setSecondaryShadowDrawable(R.drawable.shadow);
		slidingMenu.setFadeDegree(0.35f);
		
		slidingMenu.findViewById(R.id.btn4).setOnClickListener(listener);
		slidingMenu.findViewById(R.id.btn5).setOnClickListener(listener);
		slidingMenu.findViewById(R.id.btn6).setOnClickListener(listener);
		slidingMenu.findViewById(R.id.btn7).setOnClickListener(listener);
		slidingMenu.findViewById(R.id.btn8).setOnClickListener(listener);
		slidingMenu.findViewById(R.id.btn9).setOnClickListener(listener);
		slidingMenu.findViewById(R.id.btn10).setOnClickListener(listener);
		slidingMenu.findViewById(R.id.btn11).setOnClickListener(listener);
		slidingMenu.findViewById(R.id.btn12).setOnClickListener(listener);
		slidingMenu.findViewById(R.id.btn13).setOnClickListener(listener);
		slidingMenu.findViewById(R.id.btn14).setOnClickListener(listener);
		slidingMenu.findViewById(R.id.btn15).setOnClickListener(listener);
		slidingMenu.findViewById(R.id.btn16).setOnClickListener(listener);
	
		sendHttpRequest();
	}
	
	private void sendHttpRequest() {
		MgqDataHandler loginHandler = new MgqDataHandler(this, true, false) {
			@Override
			public void onSuccess(String response) {
				
				JSONObject json = JSON.parseObject(response);
				if(json.keySet().size() > 0)
				{
					slidingMenu.findViewById(R.id.btn10).setVisibility(View.VISIBLE);
					((TextView)slidingMenu.findViewById(R.id.btn10)).setText(json.getString("deviceName"));
					((TextView)slidingMenu.findViewById(R.id.btn9)).setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
					slidingMenu.findViewById(R.id.btn9).setOnClickListener(null);
				}
			}
		};
		
		JSONObject obj = ((DemoApp)getApplicationContext()).getSeletedBaby();
		if(obj == null) return;
		
		JSONObject params = new JSONObject();
		params.put("mia_un", PreferencesUtils.getString(this, "username"));
		params.put("pwd", PreferencesUtils.getString(this, "password"));
		params.put("babyid", (((DemoApp)getApplicationContext()).getSeletedBaby().getString("babyid")));

		MgqRestClient.postBody(Constant.SERVER_URL + "/sw/DownLoadDeviceInfo.ashx", JSON.toJSONString(params), loginHandler);
	}
	
	void menuBtn(View arg0)
	{
		slidingMenu.toggle(false);
		
		switch(arg0.getId())
		{
		case R.id.btn5:
			startActivity(new Intent(this,NotificationActivity.class));
			break;

		case R.id.btn6:
			startActivity(new Intent(this,GeneralActivity.class));
			break;
			
		case R.id.btn4:
			startActivity(new Intent(this,AccountActivity.class));
			break;
			
		case R.id.btn9:
			startActivity(new Intent(this,AddDeviceActivity.class));
			break;
			
		case R.id.btn10:
		case R.id.btn11:
		case R.id.btn12:
		case R.id.btn13:
		case R.id.btn14:
		case R.id.btn15:
		case R.id.btn16:
		{
			Intent intent = new Intent(this,DeviceInfoActivity.class);
			intent.putExtra("obj", (String)arg0.getTag());
			startActivity(intent);
			break;
		}
		}
	}
	
	@Override
	public void onClick(View arg0) {

		switch(arg0.getId())
		{
		case R.id.icon:
			startActivity(new Intent(this,BabyViewActivity.class));
			break;
			
		case R.id.right:
			slidingMenu.toggle(true);
			break;
			
		case R.id.btn0:
			((Button)findViewById(R.id.btn0)).setBackgroundColor(Color.rgb(13,128,214)) ;
			((Button)findViewById(R.id.btn0)).setTextColor(Color.WHITE);
			((Button)findViewById(R.id.btn1)).setBackgroundColor(Color.rgb(15,192,219));
			((Button)findViewById(R.id.btn1)).setTextColor(Color.rgb(167,222,231));
			{
				FragmentManager fm = getFragmentManager();  
		        FragmentTransaction transaction = fm.beginTransaction();  
		        transaction.replace(R.id.id_fragment_content, historyViewActivity);  
		        transaction.commit();  
			}
			break;
			
		case R.id.btn1:
			((Button)findViewById(R.id.btn0)).setBackgroundColor(Color.rgb(15,192,219));;
			((Button)findViewById(R.id.btn0)).setTextColor(Color.rgb(167,222,231));
			((Button)findViewById(R.id.btn1)).setBackgroundColor(Color.rgb(13,128,214));
			((Button)findViewById(R.id.btn1)).setTextColor(Color.WHITE);
			{
				FragmentManager fm = getFragmentManager();  
		        FragmentTransaction transaction = fm.beginTransaction();  
		        if(babyChatActivity == null) babyChatActivity = new BabyChatActivity();
		        transaction.replace(R.id.id_fragment_content, babyChatActivity);  
		        transaction.commit();  
			}
			break;
		}
	}
	
	public void startActivityForBaby(int index)
	{
		switch(index)
		{
		case 0:
			startActivityForResult(new Intent(this,AddProfileWeightActivity.class), 10);
			break;
			
		case 1:
			startActivityForResult(new Intent(this,AddProfileHeightActivity.class), 11);
			break;
		}
	}
	
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (data == null)
			return;
		
		babyChatActivity.onActivityResult(requestCode, resultCode, data);
		super.onActivityResult(requestCode, resultCode, data);
	}
}
